//
//  AffirmOrdeGoodsNumberView.m
//  Yanwei
//
//  Created by weijunjie on 2018/10/16.
//  Copyright © 2018年 DCloud. All rights reserved.
//

#import "AffirmOrdeGoodsNumberView.h"
#import "UIView+XWAddForRoundedCorner.h"
#import "GoodsModel.h"
#import "UITextField+Heaper.h"
@interface AffirmOrdeGoodsNumberView ()
@property (nonatomic, strong) UIButton *minusBtn;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UITextField *numberTextField;
@property (nonatomic, assign, getter=isShowToolView) BOOL showToolView;
@end
@implementation AffirmOrdeGoodsNumberView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.addBtn addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.minusBtn addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
        self.numberTextField.text = @"1";
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textFieldDidChangeValue:)
                                                     name:UITextFieldTextDidChangeNotification
                                                   object:self.numberTextField];
    }
    return self;
}

- (void)setModel:(GoodsModel *)model {
    _model = model;
    
    // 当前商品的数量
    self.numberTextField.text = [NSString stringWithFormat:@"%@", @(model.number)];
    
    // 商品数量大于最低购买量,可以减去商品数量
    if (model.number > model.minimum) {
        [self minusBtnEnabled:YES];
    } else {
        [self minusBtnEnabled:NO];
    }
    
    if (model.number < model.inventoryCount) {
        [self addBtnEnabled:YES];
    } else {
        [self addBtnEnabled:NO];
    }
}

#pragma mark - 懒加载
- (UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [self createBtn];
        [_addBtn setTitle:@"+" forState:UIControlStateNormal];
        [_addBtn xw_roundedCornerWithCornerRadii:CGSizeMake(1, 1) cornerColor:[UIColor whiteColor] corners:UIRectCornerTopRight | UIRectCornerBottomRight borderColor:Color34 borderWidth:0.5];
        [self addSubview:_addBtn];
        [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(40);
            make.top.bottom.right.equalTo(self);
        }];
    }
    return _addBtn;
}

- (UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [self createBtn];
        [_minusBtn setTitle:@"-" forState:UIControlStateNormal];
        [_minusBtn xw_roundedCornerWithCornerRadii:CGSizeMake(1, 1) cornerColor:[UIColor whiteColor] corners:UIRectCornerTopLeft | UIRectCornerBottomLeft borderColor:Color217 borderWidth:0.5];
        _minusBtn.userInteractionEnabled = NO;
        [_minusBtn setTitleColor:Color217 forState:UIControlStateNormal];
        [self addSubview:_minusBtn];
        [_minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(40);
            make.top.bottom.left.equalTo(self);
        }];
    }
    return _minusBtn;
}

- (UITextField *)numberTextField {
    if (!_numberTextField) {
        _numberTextField = [[UITextField alloc] init];
        _numberTextField.textColor = Color34;
        _numberTextField.font = APPFONT(14);
        _numberTextField.textAlignment = NSTextAlignmentCenter;
        _numberTextField.keyboardType = UIKeyboardTypeNumberPad;
        _numberTextField.canMove = YES;
        _numberTextField.heightToKeyboard = 55;
        [_numberTextField xw_roundedCornerWithCornerRadii:CGSizeZero cornerColor:Color34 corners:UIRectCornerAllCorners borderColor:Color34 borderWidth:0.5];
        [self addSubview:_numberTextField];
        [_numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(51);
            make.top.bottom.centerX.equalTo(self);
        }];
    }
    return _numberTextField;
}

- (UIButton *)createBtn {
    UIButton *btn = [[UIButton alloc] init];
    btn.titleLabel.font = APPFONT(14);
    [btn setTitleColor:Color34 forState:UIControlStateNormal];
    return btn;
}

#pragma mark - event
- (void)btnEvent:(UIButton *)btn {
    GoodsModel *newModel = [self.model copy];
    if ([btn.titleLabel.text isEqualToString:@"+"]) {
        newModel.number += 1;
    } else {
        newModel.number -= 1;
    }
    CGFloat totalPrice = newModel.number * [newModel.sellingPrice floatValue];
    NSString *totalPriceString = [NSString stringWithFormat:@"%.2f", totalPrice];
    if (self.goodsNumberChangedBlock) {
        self.goodsNumberChangedBlock(totalPriceString, newModel);
    }
}

- (void)addBtnEnabled:(BOOL)enabled {
    if (enabled) {
        [self.addBtn xw_roundedCornerWithCornerRadii:CGSizeMake(1, 1) cornerColor:[UIColor whiteColor] corners:UIRectCornerTopRight | UIRectCornerBottomRight borderColor:Color34 borderWidth:0.5];
        self.addBtn.userInteractionEnabled = YES;
        [self.addBtn setTitleColor:Color34 forState:UIControlStateNormal];
    } else {
        [self.addBtn xw_roundedCornerWithCornerRadii:CGSizeMake(1, 1) cornerColor:[UIColor whiteColor] corners:UIRectCornerTopLeft | UIRectCornerBottomLeft borderColor:Color34 borderWidth:0.5];
        self.addBtn.userInteractionEnabled = NO;
        [self.addBtn setTitleColor:Color217 forState:UIControlStateNormal];
    }
}

- (void)minusBtnEnabled:(BOOL)enabled {
    if (enabled) {
        [self.minusBtn xw_roundedCornerWithCornerRadii:CGSizeMake(1, 1) cornerColor:[UIColor whiteColor] corners:UIRectCornerTopLeft | UIRectCornerBottomLeft borderColor:Color34 borderWidth:0.5];
        self.minusBtn.userInteractionEnabled = YES;
        [self.minusBtn setTitleColor:Color34 forState:UIControlStateNormal];
    } else {
        [self.minusBtn xw_roundedCornerWithCornerRadii:CGSizeMake(1, 1) cornerColor:[UIColor whiteColor] corners:UIRectCornerTopLeft | UIRectCornerBottomLeft borderColor:Color217 borderWidth:0.5];
        self.minusBtn.userInteractionEnabled = NO;
        [self.minusBtn setTitleColor:Color217 forState:UIControlStateNormal];
    }
}

- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.numberTextField];
}

#pragma mark - 监听键盘高度

//- (void)notificationKeyboardWillShow:(NSNotification*)sender {
//    self.showToolView = YES;
//    if (!self.numberTextField.isFirstResponder) {
//        self.showToolView = NO;
//        return;
//    }
//    if (![APP.rootViewController.navigationController.topViewController isKindOfClass:NSClassFromString(@"AffirmOrderViewController")]) {
//        return;
//    }
//    CGRect rect = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    if (self.goodsNumberKeyboardBlock) {
//        self.goodsNumberKeyboardBlock(rect.size.height);
//    }
//}
//
//- (void)notificationKeyboardWillHide:(NSNotification*)sender {
//    if (!self.showToolView) {
//        return;
//    }
//    if (![APP.rootViewController.navigationController.topViewController isKindOfClass:NSClassFromString(@"AffirmOrderViewController")]) {
//        return;
//    }
//    if (self.goodsNumberKeyboardBlock) {
//        self.goodsNumberKeyboardBlock(0);
//    }
//}

- (void)textFieldDidChangeValue:(NSNotification *)notification
{
//    if (![APP.rootViewController.navigationController.topViewController isKindOfClass:NSClassFromString(@"AffirmOrderViewController")]) {
//        return;
//    }
//    if (self.recordGoodsInfoBlock) {
//        self.recordGoodsInfoBlock((UITextField *)[notification object], [self.model copy]);
//    }
}

@end
